//
//  WHEditMoneyCell.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/4.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHEditMoneyCell.h"

@interface WHEditMoneyCell ()

@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) UIButton *desBtn;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UIButton *addBtn;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation WHEditMoneyCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _money = 0.1;
        [self setUpEditMoneyUI];
    }
    return self;
}

- (void)setUpEditMoneyUI {
    [self.contentView addSubview:({
        _tipsLabel = [[UILabel alloc] init];
        _tipsLabel.textColor = CFontTitleBlackColor;
        _tipsLabel.font = SYSTEMFONT(kRealwidth(16));
        _tipsLabel.text = @"包裹存管费";
        _tipsLabel;
    })];
    [_tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(kRealwidth(15));
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(kRealwidth(16));
    }];
    
    [self.contentView addSubview:({
        _desBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_desBtn setImage:IMAGE_NAMED(@"shop_des_icon") forState:UIControlStateNormal];
        
        _desBtn;
    })];
    [_desBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_tipsLabel.mas_right);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.width.height.mas_equalTo(kRealwidth(55));
    }];
    
    [self.contentView addSubview:({
        _moneyLabel = [[UILabel alloc] init];
        _moneyLabel.textColor = CFontTitleBlackColor;
        _moneyLabel.font = SYSTEMFONT(kRealwidth(16));
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        _moneyLabel;
    })];
    [_moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_desBtn.mas_right);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(kRealwidth(16));
        make.width.mas_equalTo(kRealwidth(70));
    }];
    
    [self.contentView addSubview:({
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:IMAGE_NAMED(@"shop_add_icon") forState:UIControlStateNormal];
        
        _addBtn;
    })];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_moneyLabel.mas_right);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.width.height.mas_equalTo(kRealwidth(55));
    }];
    
    [_desBtn addTarget:self action:@selector(des:) forControlEvents:UIControlEventTouchUpInside];
    [_addBtn addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    
    [_desBtn addTarget:self action:@selector(startDesTimer) forControlEvents:UIControlEventTouchDown];
    [_desBtn addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchCancel];
    [_desBtn addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchUpInside];
    [_desBtn addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchUpOutside];
    
    [_addBtn addTarget:self action:@selector(startAddTimer) forControlEvents:UIControlEventTouchDown];
    [_addBtn addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchCancel];
    [_addBtn addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchUpInside];
    [_addBtn addTarget:self action:@selector(stopTimer) forControlEvents:UIControlEventTouchUpOutside];
    
}

- (void)des:(UIButton *)button {
    double money = _money-0.1;
    [self changeByMoney:money];
}

- (void)add:(UIButton *)button {
    double money = _money+0.1;
    [self changeByMoney:money];
}

- (void)changeByMoney:(double)money {
    if (money < 0.1) {
        self.money = 0.1;
    } else if (money > 3.0) {
        self.money = 3.0;
    } else {
        self.money = money;
    }
    if (self.moneyChange) {
        self.moneyChange(_money);
    }
}

- (void)setMoney:(double)money {
    _money = money;
    _moneyLabel.text = NSStringFormat(@"%.1f元", _money);
}

- (void)startDesTimer {
    __block double money = _money;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.1f block:^(NSTimer * _Nonnull timer) {
        money = money - 0.1;
        [self changeByMoney:money];
    } repeats:YES];
    
    _timer = timer;
    
}

- (void)startAddTimer {
    __block double money = _money;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.1f block:^(NSTimer * _Nonnull timer) {
        money = money + 0.1;
        [self changeByMoney:money];
    } repeats:YES];
    
    _timer = timer;
    
}

- (void)stopTimer {
    [_timer invalidate];
    _timer = nil;
}

- (void)dealloc {
    DLog(@"WHEditMoneyCell");
}

@end

